package com.myqm.controller.weixin;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.myqm.dto.ResultResponse;
import com.myqm.pojo.weixin.OriginalOrder;
import com.myqm.pojo.weixin.query.QueryOriginalOrder;
import com.myqm.service.weixin.OriginalOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@Api(tags = "原始订单")
@RestController
@RequestMapping(value = "/originalOrder/weixin")
public class OriginalOrderController extends BaseController{

	@Autowired
	public  OriginalOrderService originalOrderService;
	
	public static class AddOrder extends OriginalOrder{
		boolean delCarFlag;

		public boolean isDelCarFlag() {
			return delCarFlag;
		}

		public void setDelCarFlag(boolean delCarFlag) {
			this.delCarFlag = delCarFlag;
		}
		
	}

	@ApiOperation(value="新增记录")
	@PostMapping(value="/add")
	@ApiParam(required = true, value = "记录值")
	public ResultResponse<OriginalOrder> add(@Valid  @RequestBody AddOrder record ) {
		
		Integer customerId = this.getSelfCustomerId();
		record.setCustomerId(customerId);
				
		return new ResultResponse<OriginalOrder>(originalOrderService.add(record,customerId,record.isDelCarFlag()));
	}
	

	@ApiOperation(value = "根据ID查询订单及其明细") 
	@PutMapping(value = "/getOrder")
	public ResultResponse<OriginalOrder> getById(
			@ApiParam(required = true, value = "查询记录编号")
			@RequestBody @Valid Integer id) {
		return new ResultResponse<OriginalOrder>(originalOrderService.getById(id));
	}


	
	@ApiOperation(value = "客户订单查询") 
	@PostMapping("/customerOrder")
	public ResultResponse<PageInfo<OriginalOrder>> getByCustomer
			(@ApiParam(required = false, value = "查询参数")
			@Valid @RequestBody QueryOriginalOrder param) {
		param.setCustomerId(this.getSelfCustomerId());
		 return new  ResultResponse<PageInfo<OriginalOrder>>(originalOrderService.getbyCustomerId(param.getPageNum(),param.getPageSize(),param));
	}
	
	@ApiOperation(value = "取消订单") 
	@PutMapping(value = "/cancel")
	public ResultResponse<Integer> cancel(
			@ApiParam(required = true, value = "订单记录")
			@RequestBody @Valid Integer id) {
		return new ResultResponse<Integer>(originalOrderService.cancel(id));
	}
	
	@ApiOperation(value = "拷贝订单到购物车") 
	@PutMapping(value = "/addToCar")
	public ResultResponse<Integer> copyToCar(
			@ApiParam(required = true, value = "订单编号")
			@RequestBody @Valid Integer id) {
		return new ResultResponse<Integer>(originalOrderService.copyToCar(id));
	}
	
	@ApiOperation(value = "详细列表查询") 
	@PostMapping("/listOrder")
	public ResultResponse<PageInfo<OriginalOrder>> list
			(@ApiParam(required = false, value = "查询参数")
			@Valid @RequestBody QueryOriginalOrder param) {
		
		 return new  ResultResponse<PageInfo<OriginalOrder>>(originalOrderService.list(param.getPageNum(),param.getPageSize(),param));
	}

}

